﻿using System;
using System.Linq;
using System.Web.Mvc;
using KPIS.GERP.Models.ICM;
using KPIS.GERP.WEB.GIMS.Class;
using KPIS.GERP.WEB.GIMS.MasResourceService;
using Resources;

namespace KPIS.GERP.WEB.GIMS.Controllers
{
    // [AccessDeniedAuthorize]
    public class MasResourceGroupController : Controller
    {
        MasResourceServiceClient s = new MasResourceServiceClient();
        
        #region resource Group
        public ActionResult Index(int resTypeId)
        {
            // Resource Group 
            var model = s.SelectResourceGroup(new MasResourceGroup{RecordStatus = "!D", ResTypeId = resTypeId}, null);
            ViewBag.resTypeId = resTypeId;

            return View(model); 
        }

		public ActionResult Details(int id)
		{
			var model = s.SelectResourceGroup(new MasResourceGroup {ResGrpId = id}, null).FirstOrDefault();
            if(model == null) return new HttpNotFoundResult("ไม่พบข้อมูล");
		    int resTypeId = model.ResTypeId.Value;
            ViewBag.resTypeId = resTypeId;

			return View(model);
		}

		public ActionResult Create(int resTypeId)
		{
		    var model = new MasResourceGroup();
            var resType = s.SelectResourceType(new MasResourceType{RecordStatus = "!D", ResTypeId = resTypeId}, null).FirstOrDefault();
		    model.ResTypeId = resTypeId;
		    model.MinPriceCalDepreciation = 1;
		    ViewBag.resTypeId = resTypeId;
		    ViewBag.resType = resType;

			return View(model);
		}

		[HttpPost]
		[ValidateAntiForgeryToken]
		public ActionResult Create(MasResourceGroup model)
		{
            model = VarifyModel(model);
			if (ModelState.IsValid)
			{
				var id = s.InsertResourceGroup(model, GimsMembership.CurrentEmpId);
				return RedirectToAction("Details", new {id, model.ResTypeId});
			}

		    ViewBag.resTypeId = model.ResTypeId;

			return View(model);
		}

		public ActionResult Edit(int id, int resTypeId)
		{
			var model = s.SelectResourceGroup(new MasResourceGroup {ResGrpId = id}, null).FirstOrDefault();
		    ViewBag.resTypeId = resTypeId;

			return View(model);
		}

		[HttpPost, ValidateAntiForgeryToken]
		public ActionResult Edit(int id, MasResourceGroup model, int resTypeId)
		{
			if (ModelState.IsValid)
			{
				s.UpdateResourceGroup(model, GimsMembership.CurrentEmpId);

				return RedirectToAction("Details", new {id, resTypeId});
			}

            ViewBag.resTypeId = resTypeId;

			return View(model);
		}

		public ActionResult Delete(int id, int resTypeId)
		{
		    s.DeleteResourceGroup(id, GimsMembership.CurrentEmpId);
		    ViewBag.resTypeId = resTypeId;

		    return RedirectToAction("Index", new{resTypeId});
		}

        [HttpPost, ValidateAntiForgeryToken]
		public ActionResult DeleteAll(int[] deleteIds)
		{
			if (deleteIds == null)
				return Json(new { success = true });

			try
			{
				foreach (int delete in deleteIds)
				{
					s.DeleteResourceGroup(delete, GimsMembership.CurrentEmpId);
				}

				ModelState.AddModelError(ErrorLocalization.Error, string.Format("ลบข้อมูลไปทั้งหมด {0} รายการ", deleteIds.Length.ToString("#,##0")));
				return Json(new {success = true});
			}
			catch (Exception ex)
			{
				ModelState.AddModelError(ErrorLocalization.Error, ErrorLocalization.ErrorWhileDeleteData);
				Models.Class.GerpLog.Error(ErrorLocalization.ErrorWhileDeleteData + ", " + ex.Message);
				return Json(new {success = false});
			}
		}
        
        private MasResourceGroup VarifyModel(MasResourceGroup model)
        {
            if (string.IsNullOrWhiteSpace(model.Name))
            {
                ModelState.AddModelError("Name", "กรุณากรอกชื่อหมวด");
            }

            if(model.ResTypeId == 0 || model.ResTypeId == null || s.SelectResourceType(new MasResourceType {ResTypeId = model.ResTypeId.Value, RecordStatus = "!D"}, null).FirstOrDefault() == null)
            {
                ModelState.AddModelError("ResTypeId", "ไม่มีข้อมูลประเภท");
            }

            return model;
        }

#endregion

    }
}
